/**
 * 
 */
package autoUpdater;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.swing.JOptionPane;

import autoUpdater.gui.UpdaterFrame;
import autoUpdater.helper.Downloader;
import autoUpdater.helper.JarStarter;
import autoUpdater.helper.OnlineStatus;
import autoUpdater.helper.Util;
import autoUpdater.helper.OwnTrustManager;

/**
 * @author Christian
 * 
 */
public class AutoUpdaterMain {
	
	/**
	 * 
	 */
	private static String version = "1.3";
	
	/**
	 * 
	 */
	private File fileZumUpdaten = null;
	
	/**
	 * Serveradresse
	 */
	private  String serverUrlRelease = "https://alarmapp.chrku.de/gwtool/updater/index.php";

	
	/**
	 * Channel pw 
	 */
	private String channelPw = "";
	
	/**
	 * 
	 */
	private static boolean debugmode = true;
	
	
	/**
	 * 
	 * @author Christian
	 * @param args
	 */
	public AutoUpdaterMain(String[] args) {

		for (int i = 0; i < args.length; i++) {
			if (args[i].startsWith("-jHeldenJar=")) {
				try {
					fileZumUpdaten = new File(URLDecoder.decode(args[i].substring("-jHeldenJar=".length()), "UTF-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace(); //darf nicht passieren
				}
			}
			if (args[i].startsWith("-jChannelPw=")) {
				try {
				channelPw  = URLDecoder.decode(args[i].substring("-jChannelPw=".length()), "UTF-8");
				
				} catch (Exception e) {
					e.printStackTrace(); //darf nicht passieren
				}
			}
		}
		if(fileZumUpdaten == null) {
			JOptionPane.showMessageDialog(null, "Muss vom Heldentool aus gestartet werden");
			System.exit(0);
		}
		
		try {
			enableOwnTrustManager();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		UpdaterFrame frame = new UpdaterFrame(version);
		frame.setVisible(true);
		try {
			frame.setStatusText("Lade Onlinestatus");
			OnlineStatus oStatus = new OnlineStatus(serverUrlRelease, channelPw);
			if (fileZumUpdaten.exists()) {
				if (Util.getMd5(fileZumUpdaten).equals(oStatus.getMd5Hash())) {
					frame.dispose();
					JOptionPane.showMessageDialog(frame, "Kein Update notwendig!");
				} else {
					Downloader dler = new Downloader(oStatus, fileZumUpdaten, frame);
					Thread t = new Thread(dler);
					t.start();
					t.join();
					frame.dispose();
				}
			} else {
				JOptionPane.showMessageDialog(frame, "Jar nicht gefunden!");	
			}			
			
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(frame, "Server nicht erreichbar!");
		}
		frame.dispose();
		JarStarter starter = new JarStarter();
		starter.startHelden(fileZumUpdaten);
	}
	
	public static boolean isDebugMode() {
		
		return debugmode;
	}
	
	/**
	 * @throws Exception
	 */
	private void enableOwnTrustManager() throws Exception {
		// Create a trust manager that does not validate certificate chains
		TrustManager[] trustAllCerts = new TrustManager[] { new OwnTrustManager() };

		// Install the all-trusting trust manager
		try {
			SSLContext sc = SSLContext.getInstance("SSL");
			sc.init(null, trustAllCerts, new java.security.SecureRandom());
			HttpsURLConnection
					.setDefaultSSLSocketFactory(sc.getSocketFactory());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		new AutoUpdaterMain(args);
	}
}
